﻿// -----------------------------------------------------------------------
// <copyright file="EqualizerLogic.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Metal.Player.Equalizer
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class EqualizerLogic
    {
        #region Members

        /// <summary> Stores current instance </summary>
        private static EqualizerLogic instance = null;

        /// <summary> Stores our equalizer values </summary>
        private int[] bandArray;

        #endregion

        #region Constructor

        /// <summary> Gets the current instance of this class (Singleton - Thread safety) </summary>
        public static EqualizerLogic Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new EqualizerLogic();
                }

                return instance;
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the band array values
        /// </summary>
        public int[] BandArray
        {
            get
            {
                return this.bandArray;
            }

            set
            {
                this.bandArray = value;
            }
        }

        #endregion

        #region Methods called by reflection

        /// <summary>
        /// Show audio settings
        /// </summary>
        public void AudioSettings()
        {
            try
            {
                Metal.Config.SettingsForm settings = new Config.SettingsForm(Config.SettingsForm.ShowTab.AudioSettings);
                settings.ShowDialog();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Show LoadPreset window
        /// </summary>
        public void LoadPreset()
        {
            try
            {
                Metal.CommonForms.LoadSavePreset presetsForm = new CommonForms.LoadSavePreset(CommonForms.LoadSavePreset.ShowTab.Load);
                presetsForm.ShowDialog();

                if (presetsForm.GetPresetValues() != null)
                {
                    this.BandArray = presetsForm.GetPresetValues();

                    EqualizerController.View.Band = this.BandArray;
                    this.UpdateEqualizerValues();
                }

                presetsForm.Dispose();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        /// <summary>
        /// Show SavePreset window
        /// </summary>
        public void SavePreset()
        {
            try
            {
                Metal.CommonForms.LoadSavePreset presetsForm = new CommonForms.LoadSavePreset(CommonForms.LoadSavePreset.ShowTab.Save, this.BandArray);
                presetsForm.ShowDialog();

                presetsForm.Dispose();
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        #endregion

        #region Private methods

        /// <summary>
        /// Send our values to Core player
        /// </summary>
        private void UpdateEqualizerValues()
        {
            try
            {
                for (int i = 0; i < 10; i++)
                {
                    Core.Player.Settings.Instance.SetEqualizer(i, this.BandArray[i]);
                }
            }
            catch (Exception ex)
            {
                Metal.User.UserMessage.ShowError(ex);
            }
        }

        #endregion
    }
}
